package gui.Main;

import communications.GlobalObjectId;
import communications.LocalObjectId;
import communications.ObjectDB;
import hypeerweb.HyPeerWeb;
import hypeerweb.IHyPeerWebObserver;
import hypeerweb.Node;
import java.io.ObjectStreamException;
import java.io.Serializable;

public class GUIObserver implements IHyPeerWebObserver, Serializable {
	
	private LocalObjectId localId = null;
	private GUI main = null;
	
	protected GUIObserver()
	{
		
	}
	
	public GUIObserver(GUI main)
	{
		localId = new LocalObjectId();
		ObjectDB.getSingleton().store(localId, this);
		this.main = main;
	}

	@Override
	public void nodeAddedToHyPeerWeb(HyPeerWeb web, Node newNode) {
		main.getHyPeerWebDebugger().getMapper().getNodeListing().increaseListSize();
	}

	@Override
	public void nodeRemovedFromHyPeerWeb(HyPeerWeb web, Node deadNode) {
		main.getHyPeerWebDebugger().getMapper().getNodeListing().decreaseListSize();
	}
	
	@Override
	public void clearHypeerweb()
	{
		main.getHyPeerWebDebugger().getMapper().getNodeListing().clearNodes();
	}

	@Override
	public void segmentDisconnect(HyPeerWeb dyingSegment, HyPeerWeb replacementSegment) {
		main.printToTracePanel("Hypeerweb segment disconnected");
		main.getHyPeerWebDebugger().getMapper().getNodeListing().clearNodes();
	}
	
	@Override
	public void hibernateHyPeerWeb()
	{
		main.printToTracePanel("Hypeerweb hibernate\n");
		main.disconnectFromWeb();
		main.getHyPeerWebDebugger().getMapper().getNodeListing().clearNodes();
	}

	@Override
	public Node findAnyNode()
	{
		return null;
	}

	public LocalObjectId getLocalObjectId() {
		return localId;
	}
	
	public Object writeReplace() throws ObjectStreamException
	{
		return new GUIObserverProxy(new GlobalObjectId(localId));
	}
}
